package com.datoucai.controller;

import com.alibaba.fastjson.JSON;
import com.datoucai.ModuleTypeEnums;
import com.datoucai.entity.CommentEntity;
import com.datoucai.entity.CommentParam;
import com.datoucai.mapper.CommentMapper;
import com.datoucai.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 评论Controller
 */
@RestController
@RequestMapping("/test")
@Slf4j
@RefreshScope
public class TestController {

    @Autowired
    private IUserService userService;

    @Value(value = "${dtc.test.hh:yyy}")
    private String nacosStr;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;


    @Autowired
    private CommentMapper commentMapper;

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test(){
        int userTotal = userService.countUser();
        log.trace("trace级别日志");
        log.debug("debug级别日志");
        log.info("info级别日志");
        log.warn("warn级别日志");
        log.error("error级别日志");
        // 查询一下用户总人数
        return "大头菜-用户总人数:"+userTotal;
    }

    @RequestMapping(value = "/nacos",method = RequestMethod.GET)
    public String testNacos(){
        return nacosStr;
    }


    @RequestMapping(value = "/multi/source",method = RequestMethod.GET)
    public String testMultiSource(){
        CommentParam commentParam = new CommentParam();
        commentParam.setModule(ModuleTypeEnums.WEBSITE.getCode());
        commentParam.setResourceId(70L);
        List<CommentEntity> commentEntities = commentMapper.queryCommentByParam(commentParam);

        return JSON.toJSONString(commentEntities);
    }

    @RequestMapping(value = "/threadpool",method = RequestMethod.GET)
    public String threadpool(){
        log.info("线程池信息:{}", JSON.toJSONString(threadPoolTaskExecutor));
        threadPoolTaskExecutor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    log.info("开始睡觉");
                    Thread.sleep(60000);
                    log.info("睡觉结束");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("线程名:{}",Thread.currentThread().getName());
                log.info("线程组名:{}",Thread.currentThread().getThreadGroup().getName());
            }
        });
        ThreadGroup threadGroup = threadPoolTaskExecutor.getThreadGroup();
        String name = threadGroup!=null?threadGroup.getName():"";
        log.info("线程组:{}",name);
        return String.format("核心线程数:%d, 活跃线程:%d",
                threadPoolTaskExecutor.getCorePoolSize(),
                threadPoolTaskExecutor.getActiveCount());

    }
}
